package com.yjf.ljr.activity;

import java.util.ArrayList;
import java.util.List;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.dataclass.RePayDetailDataClass;
import com.yjf.ljr.dataclass.RePayDetailDataClass.RepayDetailInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class DebtorRepaymentDetailsActivity extends IjActivity {
	private CommonAdapter mCommonAdapter;
	private List<RepayDetailInfo> mList;
	@ID("lvDebtorReapyment")
	private ListView mLvDebtorReapyment;
	private String tradeId;// 查询Id
	private String tradeDetailId;// 关联投资id
	private String applyUserId;// 申请转让id

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_debtor_repayment_details);
		initControl();
		tradeId = getIntent().getStringExtra("tradeId");
		tradeDetailId = getIntent().getStringExtra("tradeDetailId");
		applyUserId = getIntent().getStringExtra("applyUserId");
		showProgressDialog();
		new RePayDetailTask(tradeId, tradeDetailId, applyUserId).execute();
	}

	private void initControl() {
		setTitleStr("还款详情");
		setLeftBtnClick();
		mList = new ArrayList<RepayDetailInfo>();

		mCommonAdapter = new CommonAdapter(mContext, mList, R.layout.item_debtor_repayment, ViewHolder.class,
				mHandleCallBack);
		mLvDebtorReapyment.setAdapter(mCommonAdapter);
	}

	public static class ViewHolder {
		TextView tvRepaymentData;// 还款时间
		TextView tvRepaymentStatues;// 还款状态
		TextView tvRepaymentAmount;// 还款金额
		ImageView ivpricebg;// 图像背景
	}

	private HandleCallBack mHandleCallBack = new HandleCallBack() {

		@Override
		public void handle(int position, View convertView, ViewGroup parent, Object holder, Object item) {
			// TODO Auto-generated method stub
			ViewHolder viewHolder = (ViewHolder) holder;
			RepayDetailInfo info = (RepayDetailInfo) item;
			/******** UN_PROCESS:未还款PROCESSED：已还款 *****/
			if (!TextUtils.isEmpty(info.repayDate) && (info.repayDate.length() > 10)) {
				viewHolder.tvRepaymentData.setText(info.repayDate.substring(0, 10));// 还款时间
			} else {
				viewHolder.tvRepaymentData.setText(info.repayDate);// 还款时间
			}
			if (TextUtils.isEmpty(info.status)) {
				viewHolder.tvRepaymentStatues.setText("无");// 还款状态
			} else {
//				if ("UN_PROCESS".equals(info.status)) {
//					viewHolder.tvRepaymentStatues.setText("未还款");// 还款状态
//				} else if ("PROCESSED".equals(info.status)) {
//					viewHolder.tvRepaymentStatues.setText("已还款");// 还款状态
//				}
				if ("0".equals(info.status) || "UN_PROCESS".equals(info.status)) {
					viewHolder.tvRepaymentStatues.setText("未还款");// 还款状态
				} else if ("1".equals(info.status) || "PROCESSED".equals(info.status)) {
					viewHolder.tvRepaymentStatues.setText("已还款");// 还款状态
				}
			}
			if (mList.size() == (position + 1)) {
				viewHolder.ivpricebg.setVisibility(View.VISIBLE);
			} else {
				viewHolder.ivpricebg.setVisibility(View.GONE);
			}
			viewHolder.tvRepaymentAmount.setText(info.amount);// 还款金额
		}
	};

	private class RePayDetailTask extends AsyncTask<Void, Void, String> {
		RequestObject mObject = new RequestObject();
		RePayDetailDataClass dc = new RePayDetailDataClass();
		private String tradeId;// 查询Id
		private String tradeDetailId;// 关联投资id
		private String applyUserId;// 关联投资id

		public RePayDetailTask(String tradeId, String tradeDetailId, String applyUserId) {
			this.tradeId = tradeId;
			this.tradeDetailId = tradeDetailId;
			this.applyUserId = applyUserId;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			mObject.method = "rePayDetail";
			mObject.map.put("tradeId", tradeId);
			mObject.map.put("tradeDetailId", tradeDetailId);
			mObject.map.put("applyUserId", applyUserId);
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dismissProgressDialog();
			if (TextUtils.isEmpty(result)) {
				mList.addAll(dc.list);
				mCommonAdapter.notifyDataSetChanged();
			} else {
				showToast(dc.msg);
			}
		}
	}
}
